package com.ibeeking.gmc.global.g.rest.controller.v1.feign;

import com.ibeeking.found.common.constants.HeaderNounConstant;
import com.ibeeking.found.common.entity.OperatorDTO;
import com.ibeeking.found.common.entity.TenantDetailDTO;
import com.ibeeking.found.common.entity.TenantExtendInfo;
import com.ibeeking.gmc.global.api.common.dto.TenantDTO;
import com.ibeeking.gmc.global.api.common.dto.TenantListDTO;
import com.ibeeking.gmc.global.api.common.param.TenantDetailParam;
import com.ibeeking.gmc.global.api.common.param.TenantParam;
import com.ibeeking.gmc.global.g.rest.service.ITenantDetailService;
import com.ibeeking.gmc.global.g.rest.service.ITenantService;
import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.utils.data.Collections3;
import com.ibeeking.nematos.utils.data.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/g/tenant/feign/v1")
public class TenantFeignController {

    @Resource
    private ITenantService tenantServiceImpl;

    @Resource
    private ITenantDetailService tenantDetailServiceImpl;

    @PostMapping("/queryTenantById/{id}")
    public TenantDTO queryTenantById(@PathVariable("id") Integer id) {
        return this.tenantServiceImpl.queryTenantById(id);
    }

    @PostMapping("/queryTenantByCode/{code}")
    public TenantDTO queryTenantByCode(@PathVariable("code") String code) {
        return tenantServiceImpl.queryTenantByCode(code);
    }

    @PostMapping("/queryTenantIndustryById/{id}")
    public String queryTenantIndustryById(@PathVariable("id") Integer id) {
        return this.tenantServiceImpl.queryTenantIndustryById(id);
    }

    @PostMapping("/queryDetailByTenantId/{type}/{id}")
    public TenantDetailDTO queryDetailByTenantId(@PathVariable("type") Integer type, @PathVariable("id") Integer id) {
        return tenantDetailServiceImpl.queryDetailByTenantId(type, id);
    }

    @PostMapping("/addOrUpdateTenantDetail")
    public Boolean addOrUpdateTenantDetail(@RequestBody @Validated TenantDetailParam param) {
        return this.tenantDetailServiceImpl.save(param);
    }

    @PostMapping("/modifyTenant")
    public Boolean modifyTenant(@RequestBody @Validated TenantParam tenantParam) {
        return this.tenantServiceImpl.modifyTenant(tenantParam);
    }

    @PostMapping("/updateTenantExtendInfo")
    public Boolean updateTenantExtendInfo(@RequestBody @Validated TenantExtendInfo extendInfo) {
        return this.tenantServiceImpl.updateTenantExtendInfo(extendInfo);
    }

    @PostMapping("/queryTenantIds")
    public List<Integer> queryTenantIds() {
        return tenantServiceImpl.queryTenantIds();
    }

    @PostMapping("/queryUserTenantsByIds")
    public List<TenantListDTO> queryUserTenantsByIds(@RequestBody List<Integer> ids) {
        return tenantServiceImpl.queryUserTenantsByIds(ids);
    }

    @PostMapping("/queryEffectiveTenants")
    public List<TenantListDTO> queryEffectiveTenants() {
        return tenantServiceImpl.queryEffectiveTenants();
    }

    /**
     * 根据用户ids查询用户基本信息
     *
     * @return
     */
    @PostMapping("/queryByUserIds")
    public List<OperatorDTO> queryByUserIds(HttpServletRequest request, @RequestBody List<Long> userIds) {
        String tenantId = request.getHeader(HeaderNounConstant.TENANT_ID);
        if (StringUtils.isBlank(tenantId)) {
            throw new BusinessException("租户id不能为空");
        }
        if (Collections3.isEmpty(userIds)) {
            throw new BusinessException("用户id列表不能为空");
        }
        if (userIds.size() > 1000) {
            throw new BusinessException("用户id列表一次最多查1000条");
        }
        return tenantServiceImpl.queryByUserIds(Integer.valueOf(tenantId), userIds.parallelStream().distinct().collect(Collectors.toList()));
    }
}
